﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;


using ESRI.ArcGIS.DataSourcesGDB;
using PlanningGIS.Util.Convertor;
using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.DataCheck.model
{
    /// <summary>
    /// 代码异常检查（检查所有要素的分类代码属性项是否为空，格式是否正确，图形码是否正确）
    /// </summary>
    public class FcodeExceptionCheck2 : Check
    {
         public int Check(string errType,  IFeatureLayer pFeatLyr,IGeometry pGeometry,string whereClause ,string fcodename)
         {
             IFeatureClass pFeatCls = pFeatLyr.FeatureClass;

             if (StringHelper.ConvertWRTNull(fcodename).Trim() == "")
             {
                LogHelper.WriteErrLog("未设置分类代码字段！");
                 return -1;
             }

             int index = pFeatCls.FindField(fcodename);

             if (index == -1)
             {
                LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到分类代码[" + fcodename + "]字段，检查失败！");
                 return -1;
             }

             ISpatialFilter pSpatialFileter = new SpatialFilterClass();
             if (pGeometry != null) pSpatialFileter.Geometry = pGeometry;
             pSpatialFileter.WhereClause = whereClause;
             pSpatialFileter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
             pSpatialFileter.GeometryField = pFeatCls.ShapeFieldName;

             showFakePro(true);
             LogHelper.WriteLog("正在计算需要检查的要素数目...");

             int max = FeatureClassHelper.GetFeatureCountFromLayer(pFeatLyr, pSpatialFileter);// pFeatCls.FeatureCount(pSpatialFileter);

             LogHelper.WriteLog("计算完毕。共有" + max + "个要素参与检查。");
             showFakePro(false);

             if (max == 0)
             {
                 LogHelper.WriteLog("没有满足检查要求的要素存在。");
                 return -1;
             }

             setProcessBarMax(max);

             IFeatureCursor pFeatCur;
             pFeatCur = pFeatLyr.Search(pSpatialFileter, false);

             IFeature pFeature;

             int count = 0;
             int errCount = 0;

             while ((pFeature = pFeatCur.NextFeature()) != null)
            {
                if (IsStop()) break;

                setProcessBarCurrent(count);
                count++;

                if (pFeature.get_Value(index) == DBNull.Value || pFeature.get_Value(index) == null)
                {
                    RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "分类代码为空");
                    errCount++;
                    continue;
                }

                string fcodevalue = pFeature.get_Value(index).ToString();

                if (fcodevalue.Length != 9)
                {
                    RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "分类代码不是九位码");
                    errCount++;
                    continue;
                }

                bool e7 = false;
                switch (pFeatCls.ShapeType)
                {
                    case esriGeometryType.esriGeometryPoint :
                        if (fcodevalue.Substring(6, 1) != "1" && fcodevalue.Substring(6, 1) != "2") e7 = true;
                        break;

                    case esriGeometryType.esriGeometryPolyline :
                        if (fcodevalue.Substring(6, 1) != "3" && fcodevalue.Substring(6, 1) != "4") e7 = true;
                        break;

                    case esriGeometryType.esriGeometryPolygon :
                        if (fcodevalue.Substring(6, 1) != "5") e7 = true;
                        break;

                    default :
                        break;
                }
                if (e7)
                {
                    RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "图形码错误");
                    errCount++;
                    continue;
                }
            }

            finishPro.Invoke();

            DataCheckUtility.ReleaseComObject(pFeature);
            DataCheckUtility.ReleaseComObject(pFeatCur);

            LogHelper.WriteLog("代码异常检查结束。共发现" + errCount + "处错误！\n");

            return errCount;
         }
    }
}
